package com.lewis.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lewis.core.annotation.Log;
import com.lewis.core.base.controller.BaseController;
import com.lewis.core.base.domain.BaseResult;
import com.lewis.core.base.page.TableDataInfo;
import com.lewis.core.enums.BusinessType;
import com.lewis.core.utils.office.excel.ExcelUtil;
import com.lewis.system.entity.SysLogLogin;
import com.lewis.system.service.ISysLogLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author Lewis
 */
@Api(tags = "系统访问记录")
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogLoginController extends BaseController {

	@Resource
	private ISysLogLoginService sysLoginInForService;

	@ApiOperation(value = "登录日志列表")
	@GetMapping("/list")
	@Log(
			title = "登录日志",
			businessType = BusinessType.SELECT
	)
	public TableDataInfo list(SysLogLogin sysLogLogin) {
		startPage();
		return getTableData(sysLoginInForService.selectLoginInForList(sysLogLogin));
	}

	@ApiOperation(value = "导出登录日志列表")
	@Log(
			title = "登录日志",
			businessType = BusinessType.EXPORT
	)
	@GetMapping("/export")
	public BaseResult export(SysLogLogin logininfor) {
		List<SysLogLogin> list = sysLoginInForService.selectLoginInForList(logininfor);
		ExcelUtil<SysLogLogin> util = new ExcelUtil<SysLogLogin>(SysLogLogin.class);
		return util.exportExcel(list, "登录日志");
	}

	@ApiOperation(value = "删除登录日志（支持批量）")
	@Log(
			title = "登录日志",
			businessType = BusinessType.DELETE
	)
	@DeleteMapping("/{ids}")
	public BaseResult remove(@PathVariable Long[] ids) {
		return getResult(sysLoginInForService.removeByIds(Arrays.asList(ids)));
	}

	@ApiOperation(value = "清空登录日志")
	@Log(
			title = "登录日志",
			businessType = BusinessType.CLEAN
	)
	@DeleteMapping("/clean")
	public BaseResult clean() {
		return getResult(sysLoginInForService.remove(new LambdaQueryWrapper<>()));
	}
}
